<template>
  <view class="container">
    <view class="addres-list">
      <view class="address-item" v-for="(item, index) in addresslist" :key="index">
        <view class="contacts">
          <text class="name">{{ item.consignee }}</text>
          <text class="phone">{{ item.phone }}</text>
        </view>
        <view class="address">
          <!-- <text class="region" v-for="(region, idx) in item.region" :key="idx">{{ region }}</text> -->
          <text class="detail">{{ item.address }}</text>
        </view>
        <view class="line"></view>
        <view class="item-option">
          <view class="_left">
            <label class="item-radio" @click.stop="handleSetDefault(item)">
              <radio class="radio" color="#fa2209" :checked="item.isDefault=='Y'?true:false"></radio>
              <text class="text">{{ item.isDefault=='Y'? '已设为默认' : '设为默认' }}</text>
            </label>
          </view>
          <view class="_right">
            <view class="events">
              <view class="event-item" @click="handleUpdate(item)">
                <text class="iconfont icon-edit"></text>
                <text class="title">编辑</text>
              </view>
              <view class="event-item" @click="handleRemove(item.addressId)">
                <text class="iconfont icon-delete"></text>
                <text class="title" style="color: red;">删除</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 底部操作按钮 -->
    <view class="footer-fixed">
      <view class="btn-wrapper">
        <view class="btn-item btn-item-main" @click="handleCreate()">添加新地址</view>
      </view>
    </view>
  </view>
</template>

<script>
	import {
		sysAddrList,
		sysAddrRemove
	} from '@/api/home.js'
  export default {

    data() {
      return {
        addresslist: [],
		queryParams: {
			pageNum: 1,
			pageSize: 1000,
			userId: JSON.parse(uni.getStorageSync("userInfo")).userId,
		},
		
      }
    },

    onLoad() {
      // 获取页面数据
      // this.getAddressList(),
	  this.onReorder()
	  this.getList()
    },
	

    methods: {
		getList(){
			sysAddrList(this.queryParams).then(res=>{
				this.addresslist=res.rows
			})
		},

		
      // 获取收货
      getAddressList() {
        // this.addresslist = uni.getStorageSync('savedData') || [];
      },

      // 列表排序把默认收货地址放到最前
      onReorder() {
      },
	  // 添加地址
      handleCreate() {
		  
        uni.navigateTo({
        	url: '/pages/address/editaddress',
        });
      },

     // 编辑地址
     handleUpdate(address) {
       const addressStr = encodeURIComponent(JSON.stringify(address))
       uni.navigateTo({
         url: `/pages/address/editaddress?data=${addressStr}`,
       })
     },


     // 删除地址
     handleRemove(addressId) {
       const that = this
       uni.showModal({
         title: "提示",
         content: "您确定要删除当前收货地址吗?",
         success: function (res) {
           if (res.confirm) {
             sysAddrRemove(addressId).then(resp => {
               if (resp.code === 200) {
                 uni.showToast({
                   title: '删除成功',
                   duration: 2000
                 })
				   that.getList()
                 that.getAddressList()
               } else {
                 uni.showToast({
                   title: resp.msg || '删除失败',
                   icon: 'none'
                 })
               }
             }).catch(err => {
               console.error('删除接口异常', err)
               uni.showToast({
                 title: '接口请求失败',
                 icon: 'none'
               })
             })
           }
         }
       })
     },

	  
	  // 设置默认地址
      handleSetDefault(item) {
		 const id = item.id
		  this.addresslist.forEach(item =>{
			  if(item.id == id){
				  item.address_default = true
				  return
			  }
			  item.address_default = false
		  })
        uni.setStorageSync('savedData', this.addresslist);
		this.getAddressList()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .addres-list {
    padding-top: 20rpx;
    // 设置ios刘海屏底部横线安全区域
    // padding-bottom: calc(constant(safe-area-inset-bottom) + 140rpx);
    // padding-bottom: calc(env(safe-area-inset-bottom) + 140rpx);
  }

  // 项目内容
  .address-item {
    margin: 20rpx auto 20rpx auto;
    padding: 30rpx 40rpx;
    width: 94%;
    box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
    border-radius: 16rpx;
    background: #fff;
  }

  .contacts {
    font-size: 30rpx;
    margin-bottom: 16rpx;

    .name {
      margin-right: 16rpx;
    }
  }

  .address {
    font-size: 28rpx;

    .region {
      margin-right: 10rpx;
    }
  }

  .line {
    margin: 20rpx 0;
    border-bottom: 1rpx solid #f3f3f3;
  }

  .item-option {
    display: flex;
    justify-content: space-between;
    height: 48rpx;

    // 单选框
    .item-radio {
      font-size: 28rpx;

      .radio {
        vertical-align: middle;
        transform: scale(0.76)
      }

      .text {
        vertical-align: middle;
      }
    }

    // 操作
    .events {
      display: flex;
      align-items: center;
      line-height: 48rpx;

      .event-item {
        font-size: 28rpx;
        margin-right: 26rpx;
        color: #4c4c4c;

        &:last-child {
          margin-right: 0;
        }

        .title {
          margin-left: 8rpx;
        }
      }
    }

  }


  // 底部操作栏
  .footer-fixed {
    position: fixed;
    bottom: 20px;
    left: 0;
    right: 0;
    min-height: 120rpx;
    z-index: 11;
    box-shadow: 0 -4rpx 40rpx 0 rgba(151, 151, 151, 0.24);
    background: #fff;

    // 设置ios刘海屏底部横线安全区域
    // padding-bottom: constant(safe-area-inset-bottom);
    // padding-bottom: env(safe-area-inset-bottom);

    .btn-wrapper {
      height: 120rpx;
      display: flex;
      align-items: center;
      padding: 0 40rpx;
    }

    .btn-item {
      flex: 1;
      font-size: 28rpx;
      height: 80rpx;
      line-height: 80rpx;
      text-align: center;
      color: #fff;
      border-radius: 50rpx;
    }

    .btn-item-main {
      background: linear-gradient(to right, #f9211c, #ff6335);
    }

  }
</style>
